package controller

import (
	"math/rand"
	"net/http"
	"strings"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

// 模拟用户数据
var NameList = []map[string]string{}
var count = 100

// 初始化 NameList
func init() {
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < count; i++ {
		NameList = append(NameList, map[string]string{"name": generateRandomName()})
	}
	// 添加一个固定用户
	NameList = append(NameList, map[string]string{"name": "mock-Pan"})
}

// generateRandomName 用于生成随机名字
func generateRandomName() string {
	firstNames := []string{"Alice", "Bob", "Charlie", "David", "Eva", "Frank", "Grace", "Hannah", "Ivy", "Jack"}
	return firstNames[rand.Intn(len(firstNames))]
}

// SearchUser 查找用户名
func SearchUser(c *gin.Context) {
	name := c.DefaultQuery("name", "")
	var mockNameList []map[string]string

	// 根据名字进行过滤
	for _, item := range NameList {
		if name == "" || strings.Contains(strings.ToLower(item["name"]), strings.ToLower(name)) {
			mockNameList = append(mockNameList, item)
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"items": mockNameList,
		},
	})
}

// TransactionList 获取交易列表
func TransactionList(c *gin.Context) {
	var items []map[string]interface{}
	for i := 0; i < 20; i++ {
		items = append(items, map[string]interface{}{
			"order_no":  uuid.New().String(),
			"timestamp": time.Now().UnixNano() / int64(time.Millisecond),
			"username":  generateRandomName(),
			"price":     rand.Float64()*14000 + 1000,                  // 随机价格在 1000 到 15000 之间
			"status":    []string{"success", "pending"}[rand.Intn(2)], // 随机状态
		})
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": gin.H{
			"total": 20,
			"items": items,
		},
	})
}
